Imports System.ComponentModel
Imports System.Configuration.Provider
Imports System.Web
Imports System.Web.Configuration


<DataObject(True)> _
Public Class QuestionnaireTakerODS

#Region "Initialize Part"

    Private Shared s_Initialized As Boolean
    Private Shared s_lock As New Object
    Private Shared s_Provider As QuestionnaireTakerProvider
    Private Shared s_Providers As QuestionnaireTakerProviderCollection

    Private Shared Sub Initialize()
        If Not s_Initialized Then
            'Avoid claiming lock if providers are already loaded
            If s_Provider Is Nothing Then
                SyncLock (s_lock)
                    'Get a reference to the <questionnaire> section
                    Dim section As QuestionnaireTakerSection _
                        = CType(WebConfigurationManager.GetSection("DotNetPM.Business/questionnaireTaker"), QuestionnaireTakerSection)
                    'Load registered providers and point _provider to the default provider
                    s_Providers = New QuestionnaireTakerProviderCollection()
                    ProvidersHelper.InstantiateProviders(section.Providers, s_Providers, GetType(QuestionnaireTakerProvider))
                    s_Provider = s_Providers(section.DefaultProvider)
                    If s_Provider Is Nothing Then Throw New ProviderException("Unable to load default QuestionnaireProvider")
                    s_Initialized = True
                End SyncLock
            End If
        End If
    End Sub

    Public Shared ReadOnly Property Provider() As QuestionnaireTakerProvider
        Get
            Initialize()
            Return s_Provider
        End Get
    End Property

#End Region


#Region "Shared Subs and Functions"

    <DataObjectMethod(DataObjectMethodType.Insert, True)> _
    Public Shared Function GetQuestionnaires() As DataTable
        '' TODO: Implement the security based on the Logon username
        'System.Web.HttpContext.Current.User.Identity.Name 
        Dim dt As New DataTable
        dt.Columns.Add(New DataColumn("ID"))
        dt.Columns.Add(New DataColumn("StudyID"))
        dt.Columns.Add(New DataColumn("InstrumentID"))
        dt.Columns.Add(New DataColumn("VisitNumber"))
        dt.Columns.Add(New DataColumn("Description"))
        dt.Columns.Add(New DataColumn("Completion"))
        Dim dtQuestionnaires As DataTable = QuestionnaireTakerODS.Provider.GetQuestionnaires
        Dim colId As Integer = dtQuestionnaires.Columns.IndexOf("ID")
        Dim colStudyId As Integer = dtQuestionnaires.Columns.IndexOf("StudyID")
        Dim colInstrumentId As Integer = dtQuestionnaires.Columns.IndexOf("InstrumentId")
        Dim colVisitNumber As Integer = dtQuestionnaires.Columns.IndexOf("VisitNumber")
        Dim colDescription As Integer = dtQuestionnaires.Columns.IndexOf("Description")
        Dim colEditingPermission As Integer = dtQuestionnaires.Columns.IndexOf("EditingPermission")
        Dim colTakingPermission As Integer = dtQuestionnaires.Columns.IndexOf("TakingPermission")
        For Each dr As DataRow In dtQuestionnaires.Rows
            If HttpContext.Current.User.IsInRole("QuestionnaireAdmins") Then
                Dim dtr As DataRow = dt.NewRow
                dtr.Item(0) = dr.Item(colId)
                dtr.Item(1) = dr.Item(colStudyId)
                dtr.Item(2) = dr.Item(colInstrumentId)
                dtr.Item(3) = dr.Item(colVisitNumber)
                dtr.Item(4) = dr.Item(colDescription)
                dtr.Item(5) = QuestionnaireTakerODS.Provider.GetPercentCompletion(dr.Item(colId), dr.Item(colInstrumentId))
                dt.Rows.Add(dtr)
            Else
                Dim perm As String = dr(colEditingPermission) & "|" & dr(colTakingPermission)
                Dim roles As String() = perm.Split("|")
                For Each rl As String In roles
                    If HttpContext.Current.User.IsInRole(rl) Then
                        Dim dtr As DataRow = dt.NewRow
                        dtr.Item(0) = dr.Item(colId)
                        dtr.Item(1) = dr.Item(colStudyId)
                        dtr.Item(2) = dr.Item(colInstrumentId)
                        dtr.Item(3) = dr.Item(colVisitNumber)
                        dtr.Item(4) = dr.Item(colDescription)
                        dtr.Item(5) = QuestionnaireTakerODS.Provider.GetPercentCompletion(dr.Item(colId), dr.Item(colInstrumentId))
                        dt.Rows.Add(dtr)
                        Exit For
                    End If
                Next
            End If
        Next
        Return dt
    End Function

    Public Shared Function GetQuestionnaireNode(ByVal id As Integer) As QuestionnaireNode
        Return QuestionnaireTakerODS.Provider.BuildQuestionnaireNode(id)
    End Function


#End Region

End Class
